<%--
  Created by IntelliJ IDEA.
  User: 86157
  Date: 2021/12/29
  Time: 10:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>产品生产工序</title>
</head>
<body>
<div id="lxm_sctable">

    <table id="dfiletal">

    </table>
</div>
<div id="lxm_sctable1" style="display: none" >
    <div style="position: absolute; right: 10%">
        <input type="button" onclick="btnopen()" class="btn btn-primary" value="添加工序">
        <input type="button" class="btn btn-primary" value="删除工序" onclick="delegx()">
        <input type="button" class="btn btn-primary" value="确定" id="quedingsj">
        <input type="button" class="btn btn-primary" value="返回" id="fhsc">
    </div><br><br><br>
    <form id="tjdesign" class="form form-horizontal">
    <div class="container">
        <div style="position: absolute; left: 40%"><h4>生产工序设计单</h4></div><br><br><br>


        <input type="hidden" id="id" name="id">
        产品名称：<input type="text" name="productName" id="productName" readonly="readonly" style="border: none">
        产品编号：<input type="text" name="productId" id="productId" readonly="readonly" style="border: none">

        <div>设计人：<input type="text" style="border-left-style: none;border-right-style: none;border-top-style: none"></div>
        <%--表格展示数据--%>
        <table id="showData" style="width: 900px; height: 50px;">

        </table>
        <div style="position: relative;top: -400px">
            <div>
                登记人：<input type="text" name="responsiblePerson"  id="responsiblePerson" style="border-left-style: none;border-right-style: none;border-top-style: none">
                登记时间：<input type="text" id="djDate" style="border: none"></div><br>
            <div>设计要求：<input type="text" style="width: 900px;height: 80px;"></div>
        </div>
    </form>



        <!-- 模态框  选择物料  指向 childdemo页面-->
        <div id="openwin" class="modal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    <script>
        var params_onemdes={
            url: "${pageContext.request.contextPath}/dfile.action",
            method: 'GET',                      //请求方式（*）
            toolbar: '#toolbar',              //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: true,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
            pageSize: 5,                     //每页的记录行数（*）
            pageList: [5, 10, 25, 50, 100],        //可供选择的每页的行数（*）
            search: false,                      //是否显示表格搜索
            strictSearch: true,
            showColumns: true,                  //是否显示所有的列（选择显示的列）
            showRefresh: true,                  //是否显示刷新按钮
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            height: 500,                      //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
            cardView: false,                    //是否显示详细视图
            detailView: false,                  //是否显示父子表
            //传递额外的查询用的参数
            queryParams: function (params) {
                //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                var temp = {
                    pagesize: params.limit,                         //页面大小
                    pageno: (params.offset / params.limit) + 1,   //页码
                   /* designProcedureTag:"G001-0" //工序组成标志-未设计*/
                    checkTag: "S001-1", //查询条件  审核通过
                    type:"Y001-1",  //物料组成设计  只查询产品
                    designModuleTag:"W001-1",//物料已设计
                    designProcedureTag:"G001-0" //工序组成标志-未设计
                };
                return temp;
            },
            columns:[
                {
                    title:"产品编号",
                    field:"productId"
                },
                {
                    title: "产品名称",
                    field: "productName"
                },
                {
                    title: "档次级别",
                    field: "productClass",
                    formatter:function (value) {
                        if(value=="D001-1"){
                            return"高档";
                        }else
                        if(value=="D001-2"){
                            return"中档";
                        }else
                        if (value=="D001-3"){
                            return "低档";
                        }
                    }
                },
                {
                    title: "I级分类",
                    field: "firstKindName"
                },
                {
                    title: "II级分类",
                    field: "secondKindName"
                },
                {
                    title: "III级分类",
                    field: "thirdKindName"
                },
                {
                    title: "产品经理",
                    field: "responsiblePerson"
                },
                {
                    title: "制定设计单",
                    field: "",
                    formatter:function (value,row,index) {
                        return "<input type='button' onclick='zhiding(" + row.id + ")' value='制定设计单' class='btn btn-primary'>"
                    }
                }
            ]
        };
        $("#dfiletal").bootstrapTable(params_onemdes);

        //条件查询
        $("#btnsearch").click(function () {
            //刷新表格数据
            $("#dfiletal").bootstrapTable("refresh");
        })

        var params_onemdes2={
            data:[],
            uniqueId:"procedureId",
            columns:[
                {
                    title: "全选",
                    field: "ck",
                    checkbox:true
                },
                {
                    title: "工序名称",
                    field: "procedureName"
                },
                {
                    title: "工序编号",
                    field: "procedureId"
                },
                {
                    title: "描述",
                    field: "procedureDescribe"
                },
                {
                    title: "工时数",
                    field: "labourHourAmount",
                    formatter: function (value, row, index) {
                        return "<input data-id='amount" + index + "'  type='text' class='form-control' value=''/>"
                    }
                },
                {
                    title: "工时单位",
                    field: "amountUnit",
                    formatter: function (value, row, index) {
                        return "<input data-id='amunit" + index + "'  type='text' class='form-control' value='小时' readonly='readonly'/>"
                    }
                },
                {
                    title: "单位工时成本",
                    field: "costPrice",
                    formatter: function (value, row, index) {
                        return "<input data-id='cprice" + index + "'  type='text' class='form-control' value=''/>"
                    }
                },
                {
                    title: "工时成本小计(元)",
                    field: "subtotal",
                    formatter: function (value, row, index) {
                        return "<span id='span" + index + "'></span>";
                    }
                }
            ]
        };
        $("#showData").bootstrapTable(params_onemdes2);

        var rows;
        function btnopen() {
            $.showDialogBoxByUrl("添加工序",
                "${pageContext.request.contextPath}/scgx/addgongxu.jsp",{},
                function (data) {
                    if (data==1){
                       var rows2=$("#addgongxu").bootstrapTable("getAllSelections");
                       rows=[];
                       $.each(rows2,function (index,item) {
                            var temp={"procedureId":item.typeId,"procedureName":item.typeName,"procedureDescribe":item.describe1};
                            rows.push(temp)
                       })
                        $("#showData").bootstrapTable("load",rows);
                    }
                },"md")
        }

        //删除工序
        function delegx() {
            var rows4 = $("#showData").bootstrapTable("getAllSelections");
            console.log(rows4)
            for (var i =0;i<rows4.length;i++) {
                $("#showData").bootstrapTable("removeByUniqueId", rows4[i].procedureId);
            }
        }

        //登记时间
        var dDate = new Date();
        var sDate = dDate.getFullYear() + "/" + (dDate.getMonth() + 1) + "/" + dDate.getDate();
        sDate = sDate + " " + dDate.getHours() + ":" + dDate.getMinutes() + ":" + dDate.getSeconds();
        $("#djDate").val(sDate);

        //工时成本小计
        $("#showData").on("change", function (e) {

            if (e.target.nodeName.toUpperCase() == "INPUT" && $(e.target).attr("type") == "text") {

                var str = $(e.target).attr("data-id");
                var index =parseInt( $(e.target).attr("data-id").substr(6));
                if(str.indexOf("amount")!=-1)
                {
                    var val = e.target.value;

                    var price = $(e.target).parent().next().next().find("input").val();

                    $("#span" + index).text(val * price);
                }
                else  if(str.indexOf("cprice") !=-1)
                {
                    var val = e.target.value;

                    var amount = $(e.target).parent().prev().prev().find("input").val();

                    $("#span" + index).text(val * amount);
                }
            }
        })
        //返回
        $("#fhsc").click( function () {
            $("#lxm_sctable").attr("style","display:block");
            $("#lxm_sctable1").attr("style","display:none");
        })

        var mDesignProcedure;
        function zhiding(id) {
            $.get("${pageContext.request.contextPath}/dfilebyid.action",{id:id},
                function (data) {
                $("#productName").val(data.productName);
                $("#productId").val(data.productId);
                $("#responsiblePerson").val(data.responsiblePerson);
                    mDesignProcedure={
                    firstKindId:data.firstKindId,firstKindName:data.firstKindName,
                    secondKindName:data.secondKindName,secondKindId:data.secondKindId,
                    thirdKindId:data.thirdKindId,thirdKindName:data.thirdKindName,
                    productId:data.productId,productName:data.productName

                }
            }, "json")

            $("#lxm_sctable").attr("style","display:none")
            $("#lxm_sctable1").attr("style","display:block")
        }

        //提交
        $("#quedingsj").click(function () {
            //组装物料设计明细表数据
            var designDetails = [];
            var input_amount = $("#showData input[data-id^='amount']");
            var input_amunit = $("#showData input[data-id^='amunit']");
            var input_cprice = $("#showData input[data-id^='cprice']");
            //获取工序明细数据
            rows =$("#showData").bootstrapTable("getData");

            $.each(rows, function (index, item) {
                //productId  amount
                var dd = {procedureId: item.procedureId,
                    procedureName: item.procedureName,
                    procedureDescribe:item.procedureDescribe,
                    labourHourAmount:input_amount.eq(index).val(),
                    amountUnit: input_amunit.eq(index).val(),
                    costPrice:input_cprice.eq(index).val()};
                designDetails.push(dd);
            });
            mDesignProcedure["details"] = designDetails;

            $.ajax({
                url: "${pageContext.request.contextPath}/tjdesign.action",
                type: "put",
                data: JSON.stringify(mDesignProcedure),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.code == 1) {
                        alert(data.msg)
                        $("#dfiletal").bootstrapTable("refresh")
                        $("#showData").bootstrapTable("refresh")

                        $("#lxm_sctable1").attr("style","display:none")
                        $("#lxm_sctable").attr("style","display:block")
                    } else {
                        alert(data.msg)
                    }
                }
            })


        })
    </script>
</body>
</html>
